﻿using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Baula.Testing.Common
{
    public static class AssertExtensions
    {
        public static void AssertEqualsIgnoringTrailingNewLine(this string actual, string expected)
        {
            if ((actual == null) == (expected == null))
            {
                if (actual == expected)
                {
                    return;
                }
                if (actual.EndsWith("\r\n"))
                {
                    var tweakedS1 = actual.Substring(0, actual.Length - 2);
                    if (tweakedS1 == expected)
                    {
                        return;
                    }
                }
                if (expected.EndsWith("\r\n"))
                {
                    var tweakedS2 = expected.Substring(0, expected.Length - 2);
                    if (actual == tweakedS2)
                    {
                        return;
                    }
                }
            }
            Assert.Fail(
                @"The two strings differ in more than a single trailing newline:
Actual:   <{0}>
Expected: <{1}>",
                actual, expected);
        }

        public static void AssertEqual<T>(this IEnumerable<T> actualList, IEnumerable<T> expectedList)
        {
            var expectedEnum = expectedList.GetEnumerator();

            foreach (var actualElement in actualList)
            {
                if (!expectedEnum.MoveNext())
                    Assert.Fail(string.Format("Actual list has element \"{0}\" after the end of expected list.", actualElement));
                Assert.AreEqual(expectedEnum.Current, actualElement);
            }
            if (expectedEnum.MoveNext())
                Assert.Fail(string.Format("Actual list ends without reaching expected element \"{0}\".", expectedEnum.Current));
        }
    }
}